package newapp.dob.com.dc_new_app.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;


import java.util.List;

import newapp.dob.com.dc_new_app.R;
import newapp.dob.com.dc_new_app.util.EasyPermissions;

public class BaseActivity extends RxAppCompatActivity implements EasyPermissions.PermissionCallbacks {

    // private Animation translateAnim, alphaAnim;
    private boolean statusBar;
    private TextView mTitle, mLeftTextBtn, mRightTextBtn;
    private ImageButton mLeftImgBtn, mRightImgBtn;
    private LinearLayout mContent, mStatusBar, mBaseView;
    private RelativeLayout mTitleBar;
    private Context context;
    private String activityName;
    private int viewType = 0;
    protected int reSting = R.string.ask_again;//默认提示语句
    protected static final int RC_PERM = 123;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#FFFFFF"));
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.systemUiVisibility = View.INVISIBLE;
//        getWindow().setAttributes(params);
//        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//        getWindow().getDecorView().setSystemUiVisibility(
//                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
////                        | View.SYSTEM_UI_FLAG_LOW_PROFILE
////                | View.SYSTEM_UI_FLAG_FULLSCREEN
////                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                     );
        super.onCreate(savedInstanceState);


        // 友盟统计
//        AnalyticsConfig.setAppkey(this, "583be97d677baa7c71000b1d");
//        MobclickAgent.setDebugMode(true);
//        MobclickAgent.updateOnlineConfig(this);
    }

    public static boolean checkDeviceHasNavigationBar(Context activity) {

        //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
        boolean hasMenuKey = ViewConfiguration.get(activity)
                .hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap
                .deviceHasKey(KeyEvent.KEYCODE_BACK);

        if (!hasMenuKey && !hasBackKey) {
            // 做任何你需要做的,这个设备有一个导航栏
            return true;
        }
        return false;
    }

    public static int getNavigationBarHeight(Activity activity) {
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");
        //获取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    public void initBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.TRANSPARENT);
            tintManager.setNavigationBarTintColor(Color.parseColor("#000000"));
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
//            com.githang.statusbar.StatusBarCompat.setStatusBarColor(this, Color.parseColor("#e3483d"));
        }
    }

    public void initBarTrance() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.TRANSPARENT);
            tintManager.setNavigationBarTintColor(Color.parseColor("#000000"));
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
        }
    }


    public void initSecBar() {


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.TRANSPARENT);
            tintManager.setNavigationBarTintColor(Color.parseColor("#000000"));
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
        }
//        com.githang.statusbar.StatusBarCompat.setStatusBarColor(this, Color.parseColor("#e3483d"));
    }







    public void setBaseBackground(int color) {
        mContent.setBackgroundColor(color);
    }

    public LinearLayout getBaseView() {
        return mBaseView;
    }

    public LinearLayout getStatusBar() {
        return mStatusBar;
    }

    public RelativeLayout getTitleBar() {
        return mTitleBar;
    }

    public TextView getTitleText() {
        return mTitle;
    }

    public void isToolbarVisibilty(boolean f) {
        if (f) {
            mTitleBar.setVisibility(View.GONE);
        } else {
            mTitleBar.setVisibility(View.VISIBLE);
        }
    }

    public void setTitleText(String text) {
        mTitle.setText(text);
    }

    public TextView getLeftTextBtn() {
        return mLeftTextBtn;
    }

    public void setLeftTextBtn(String text) {
        mLeftTextBtn.setText(text);
    }

    public TextView getRightTextBtn() {
        return mRightTextBtn;
    }

    public void setRightTextBtn(String text) {
        mRightTextBtn.setText(text);
    }

    public ImageButton getLeftImgBtn() {
        return mLeftImgBtn;
    }

    public void setLeftImgBtn(int resId) {
        mLeftImgBtn.setImageResource(resId);
    }

    public ImageButton getRightImgBtn() {
        return mRightImgBtn;
    }

    public void setRightImgBtn(int resId) {
        mRightImgBtn.setImageResource(resId);
    }

    //返回上一个Activity
    public void setBackIntent() {
        mLeftImgBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    // 跳转下一个Activity
    public void startNextIntent(final Context context, final Class<?> toClass) {
        mRightTextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, toClass));
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.left_enter, R.anim.right_out);
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    /**
     * 权限回调接口
     */
    private CheckPermListener mListener;

    public interface CheckPermListener {
        //权限通过后的回调方法
        void superPermission();
    }

    public void checkPermission(CheckPermListener listener, int resString, int reSting, String... mPerms) {
        this.reSting = reSting;
        mListener = listener;
        if (EasyPermissions.hasPermissions(this, mPerms)) {
            if (mListener != null)
                mListener.superPermission();
        } else {
            EasyPermissions.requestPermissions(this, getString(resString),
                    RC_PERM, mPerms);
        }
    }

    /**
     * 用户权限处理,
     * 如果全部获取, 则直接过.
     * 如果权限缺失, 则提示Dialog.
     *
     * @param requestCode  请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//        if (requestCode == EasyPermissions.SETTINGS_REQ_CODE) {
//            //设置返回
//        }
//    }


    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //同意了某些权限可能不是全部
    }

    @Override
    public void onPermissionsAllGranted() {
        if (mListener != null)
            mListener.superPermission();//同意了全部权限的回调
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

        EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
               "你未对“贷欧巴”开放" + getString(reSting) + "权限，请前往手机“设置”进行修改！",
                R.string.setting, R.string.cancel, null, perms);
    }
}
